"""
Case Type   : Timescaledb
Case Name   : 增加分区维度函数add_dimension(),int类型时间间隔分区维度使用INTERVAL定义合理报错
Create At   : 2025/02/17
Owner       : liu-tong-8848
Description :
              1.创建普通表 DROP TABLE IF EXISTS CONDITIONS cascade;
              CREATE TABLE CONDITIONS ( time timestamp not null, time_received int not null, address text,
              temperature double precision, card_id bigint );
              2.转换为超表 select create_hypertable('conditions','time');
              3.增加分区维度不指定分区数 select add_dimension('conditions','time_received',chunk_time_interval =>INTERVAL '1 day');
Expect      :
              1.普通表创建成功
              2.创建超表成功
              3.合理报错，提示 ERROR: invalid interval: must be an integer type for integer dimensions
History     :
"""

import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class Timescaledb(unittest.TestCase):
    table_name = "conditions"

    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info('==Opengauss_Function_Timescaledb_add_dimension_Case0004开始==')
        self.Constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbPGmode')
        self.db_user_node = Node(node='PrimaryDbPGmode')
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status or "Degraded" in status)
        select_extension_cmd = f"SELECT * FROM pg_extension;"
        select_extension_result = self.primary_sh.execut_db_sql(select_extension_cmd)
        if "timescaledb" not in select_extension_result:
            create_extension_cmd = f"create extension timescaledb;"
            create_extension_result = self.primary_sh.execut_db_sql(create_extension_cmd)
            self.assertIn(self.constant.extension_create_success, create_extension_result, '创建timescaledb扩展失败')

    def test_add_dimension(self):
        text = '----step1: 开启线程池，创建普通表 expect: 建表成功----'
        self.log.info(text)
        drop_table_sql = "DROP TABLE IF EXISTS " + self.table_name
        drop_table_result = self.primary_sh.execut_db_sql(drop_table_sql)
        self.assertIn(self.Constant.DROP_TABLE_SUCCESS, drop_table_result)
        create_table_sql = "CREATE TABLE " + self.table_name + " ( time timestamp not null, " \
                           + "time_received int not null, address text, " \
                           + "temperature double precision, card_id bigint );"
        create_table_result = self.primary_sh.execut_db_sql(create_table_sql)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, create_table_result,
                      "执行失败")

        text = '----step2: 转换为超表 expect: 转换成功----'
        self.log.info(text)
        create_hypertable_sql = "select create_hypertable('" + self.table_name + "','time');"
        create_hypertable_result = self.primary_sh.execut_db_sql(create_hypertable_sql)
        self.log.info(create_hypertable_result)
        self.assertIn(self.table_name, create_hypertable_result.split("\n")[-2].strip())

        text = '----step3: 增加分区维度指定分区数 expect: 成功----'
        self.log.info(text)
        add_dimension_sql = "select add_dimension('" + self.table_name + "'," \
                            "'time_received',chunk_time_interval => INTERVAL '1 day')"
        add_dimension_result = self.primary_sh.execut_db_sql(add_dimension_sql)
        self.assertIn("ERROR", add_dimension_result)
        self.assertIn("invalid interval", add_dimension_result)
        self.assertIn("must be an integer type for integer dimensions", add_dimension_result)

    def tearDown(self):
        self.log.info("恢复默认值")
        drop_table_sql = "DROP TABLE IF EXISTS " + self.table_name
        drop_table_cmd = self.primary_sh.execut_db_sql(drop_table_sql)
        self.assertIn(self.Constant.DROP_TABLE_SUCCESS, drop_table_cmd)
        self.log.info('==Opengauss_Function_Timescaledb_add_dimension_Case0004完成==')

